//# sourceURL=http://uMapApp/Module/Bridge/BridgeWeatherDetection
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
    , "./BridgeWeatherDetectionNode"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum,
    BridgeWeatherDetectionNode
) {

    function BridgeWeatherDetection(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.nodes = {};

        _this.otherLabelsGroup = [];

        if (_this.position) {

            _this.startStakeNum = StakeNum.transform(_this["开始桩号"]);
            _this.stopStakeNum = StakeNum.transform(_this["结束桩号"]);

            _this.stakeNum = _this.startStakeNum;

            _this.title = _this["名称"];
            _this.searchKey = _this["名称"] + " " + _this["类型"] + " " + _this["朝向"];
            _this.sortKey = _this.startStakeNum.distance;

            _this.layer = new Layer({
                "dataSource": _this.dataSource,
                "app"       : _this.app
            });

            _this.marker = new uMap.Entity({
                objectOption: {
                    position : _this.position,
                    billboard: new uMap.BillboardGraphic({
                        objectOption: {
                            image                     : _this.app.icons.getIconMarker(_this.app.icons.bridge),
                            verticalOrigin            : "BOTTOM"
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    }),
                    label    : new uMap.LabelGraphic({
                        objectOption: {
                            text                      : _this["名称"],
                            verticalOrigin            : "TOP"
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            });
            _this.layer.dataSources.entities.add(_this.marker.object);


            if (_this.otherLabels) {


                $.each(_this.otherLabels, function (i, label) {

                    var labelEntity = new uMap.Entity({
                        objectOption: {
                            position: label.position,
                            label   : new uMap.LabelGraphic({
                                objectOption: {
                                    text                      : label.title
                                    , disableDepthTestDistance: Number.POSITIVE_INFINITY
                                }
                            }),
                            show    : false
                        }
                    });

                    _this.layer.dataSources.entities.add(labelEntity.object);
                    _this.otherLabelsGroup.push(labelEntity);
                });

            }

        }


    }

    var _class = BridgeWeatherDetection;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"           : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"          : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle"         : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active"         : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                        _this.marker.label.scale = 1.2;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge, true);

                        _this.openNode();


                        _this.trigger("active");

                    } else {

                        _this.marker.label.fillColor = new uMap.Color(["#FFF"]);
                        _this.marker.label.scale = 1;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge);

                        _this.closeNode();

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        },
        "loadNode"       : {
            value: function (callback) {

                var _this = this;


                if (!_this.isNodeLoaded) {

                    if (_this.nodeRequest && _this.nodeMap) {

                        _this.nodeMap.send({}, function (mapJson) {

                            if (mapJson.data) {
                                _this.nodeObjMap = new mhjs.Map(mapJson.data.obj);
                                _this.nodePropertiesMap = new mhjs.Map(mapJson.data.properties);
                                _this.nodeDic = mapJson.data.dic;
                            }

                            _this.nodeRequest.send({"bridge": _this["编号"]}, function (nodeJson) {
                                if (nodeJson.data.list && nodeJson.data.list.length > 0) {

                                    $.each(nodeJson.data.list, function (i, node) {

                                        //静态数据筛选
                                        if (node.bridgeNum === _this["编号"]) {

                                            if (_this.nodeObjMap) {
                                                node = _this.nodeObjMap.format(node);
                                            }

                                            node.app = _this.app;
                                            node.dataRequest = _this.nodeDataRequest;
                                            node.dic = _this.nodeDic;
                                            node.bridgeWeatherDetection = _this;

                                            var bridgeWeatherDetectionNode = new BridgeWeatherDetectionNode(node);
                                            _this.nodes[bridgeWeatherDetectionNode["id"]] = bridgeWeatherDetectionNode;

                                        }

                                    });

                                    callback && callback();


                                    _this.isNodeLoaded = true;
                                    _this.trigger("loadNode");

                                }
                            });

                        });


                    }
                } else {
                    callback && callback();
                }
            }
        },
        "openNode"       : {
            value: function (callback) {

                var _this = this;

                _this.loadNode(function () {
                    $.each(_this.nodes, function (i, node) {
                        node.open();
                    });

                    $.each(_this.otherLabelsGroup, function (i, label) {
                        label.show = true;
                    });

                    _this.marker.show = false;

                    callback && callback();

                });

                _this.trigger("openNode");
            }
        },
        "closeNode"      : {
            value: function () {

                var _this = this;
                $.each(_this.nodes, function (i, node) {
                    node.close();
                });

                $.each(_this.otherLabelsGroup, function (i, label) {
                    label.show = false;
                });

                _this.marker.show = true;

                _this.trigger("closeNode");
            }
        },
        "activeNode"     : {
            value: function (node) {
                var _this = this;
                _this.unActiveAllNode();
                node.active(!node.isActive);
                node.statusTree.toggleClass("active", node.isActive);
                // node.typeTree.toggleClass("active", node.isActive);
            }
        },
        "unActiveAllNode": {
            value: function () {
                var _this = this;
                $.each(_this.nodes, function (i, node) {
                    node.active(false);
                    node.statusTree.removeClass("active");
                    // node.typeTree.removeClass("active");
                });
            }
        }
    });

    return _class;

});